/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pradoeditor.helpers;

import java.io.File;
import pradoeditor.comun.ItemProject;
import pradoeditor.exceptions.ProjectExistException;

/**
 *
 * @author jaimeg
 */
public class ProjectHelper
{
    public  static void createProject(String path, String projectName) throws ProjectExistException
    {
        final String _assets = "assets";
        final String _protected = "protected";
        final String _database = "database";
        final String _layouts = "layouts";
        final String _pages = "pages";
        final String _themes = "themes";

        //Crea el directorio principal del proyecto.
        File newPath = new File(path, projectName);

        //Valida que el directorio destino no exista.
        boolean exists = newPath.exists();
        if (exists)
            throw new ProjectExistException(newPath.toString());

        IOHelper.createDirectory(newPath.toString());

        //Crea el directorio assets
        File pathAssets = new File(newPath.toString(), _assets);
        IOHelper.createDirectory(pathAssets.toString());

        //Crea el directorio protected
        File pathProtected = new File(newPath.toString(), _protected);
        IOHelper.createDirectory(pathProtected.toString());

        File pathDatabase = new File(pathProtected.toString(), _database);
        IOHelper.createDirectory(pathDatabase.toString());

        File pathLayouts = new File(pathProtected.toString(), _layouts);
        IOHelper.createDirectory(pathLayouts.toString());
        
        File pathPages = new File(pathProtected.toString(), _pages);
        IOHelper.createDirectory(pathPages.toString());

        //Crea el directorio themes
        File pathThemes = new File(newPath.toString(), _themes);
        IOHelper.createDirectory(pathThemes.toString());        
    }

// <editor-fold defaultstate="collapsed" desc="Metodos para abrir un proyecto">

    public static ItemProject openProject(String path)
    {
        ItemProject lReturn = new ItemProject("Proyecto", path);

        fillChildrens(new File(path), lReturn);
        return lReturn;
    }

    private static void fillChildrens(File path, ItemProject item)
    {
        File[] files = path.listFiles();

        for (int x = 0; x < files.length; x++)
        {
            ItemProject newItemProject = new ItemProject(files[x].getName(), files[x].getPath());

            if (files[x].isDirectory())
            {
                fillChildrens(files[x], newItemProject);
            }
            item.getChildrens().add(newItemProject);
        }
    }

    // </editor-fold>
}
